package com.yanqu.road.dao.impl.activity.horserun.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.cross.CrossHorseRunTrackData;
import com.yanqu.road.entity.activity.horserun.entity.HorseRunTrackPartData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHorseRunTrackDataDaoImpl extends TempDataObjectDao<CrossHorseRunTrackData> {
    public static String getCrossHorseRunTrackDataSql = "select * from `t_c_horse_run_track_data` where `activityId`=? and `groupId`=?";
    public Map<Integer,Map<Integer, CrossHorseRunTrackData>> getCrossHorseRunTrackData(int activityId,int groupId) {
        Map<Integer,Map<Integer, CrossHorseRunTrackData>> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossHorseRunTrackDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossHorseRunTrackData parse = parse(rs);
                Map<Integer, CrossHorseRunTrackData> dayMap = result.computeIfAbsent(parse.getDay(), k -> new ConcurrentHashMap<>());
                dayMap.put(parse.getZone(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossHorseRunTrackDataSql = "update `t_c_horse_run_track_data` set totalLength=?,attrTotalLength=?,partData=?,isSettlement=?,needSettlementTime=? where activityId=? and groupId=? and day=? and zone=?";
    @Override
    protected boolean updateData(CrossHorseRunTrackData crossHorseRunTrackData) {
        crossHorseRunTrackData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getTotalLength()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunTrackData.getAttrTotalLength())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunTrackData.getPartData())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossHorseRunTrackData.isSettlement()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunTrackData.getNeedSettlementTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getDay()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getZone()));
        if (getDb().executeNoneQuery(updateCrossHorseRunTrackDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossHorseRunTrackDataSql = "insert into `t_c_horse_run_track_data`(activityId,groupId,day,zone,totalLength,attrTotalLength,partData,isSettlement,needSettlementTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossHorseRunTrackData crossHorseRunTrackData) {
        crossHorseRunTrackData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getDay()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getZone()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunTrackData.getTotalLength()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunTrackData.getAttrTotalLength())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunTrackData.getPartData())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossHorseRunTrackData.isSettlement()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunTrackData.getNeedSettlementTime()));
        if (getDb().executeNoneQuery(insertCrossHorseRunTrackDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossHorseRunTrackData parseData(ResultSet rs) throws SQLException {
        CrossHorseRunTrackData crossHorseRunTrackData = new CrossHorseRunTrackData();
        crossHorseRunTrackData.setActivityId(rs.getInt("activityId"));
        crossHorseRunTrackData.setGroupId(rs.getInt("groupId"));
        crossHorseRunTrackData.setDay(rs.getInt("day"));
        crossHorseRunTrackData.setZone(rs.getInt("zone"));
        crossHorseRunTrackData.setTotalLength(rs.getInt("totalLength"));
        crossHorseRunTrackData.setAttrTotalLength(JSON.parseObject(rs.getString("attrTotalLength"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunTrackData.setPartData(JSON.parseObject(rs.getString("partData"), new TypeReference<List<HorseRunTrackPartData>>() {}));
        crossHorseRunTrackData.setIsSettlement(rs.getBoolean("isSettlement"));
        crossHorseRunTrackData.setNeedSettlementTime(rs.getLong("needSettlementTime"));

        return crossHorseRunTrackData;

    }
}
